<template>
  <div>
    <!-- 顶部新增按钮和搜索框 -->
    <div class="btn">
      <div class="addBtn">
        <el-button type="primary" @click="toAddHos">新增</el-button>
      </div>
      <!-- 搜索区域 -->
      <div class="searchArea">
        <!-- 搜索框 -->
        <el-input placeholder="请输入医院名称" v-model="hosName" clearable>
        </el-input>
        <!-- 搜索按钮 -->
        <el-button @click="getHosData" class="searchBtn" type="primary">搜索</el-button>
      </div>
    </div>

    <!-- 表格内容展示区 -->
    <div class="tableContent">
      <el-table :data="hosData" style="width: 100%">
        <!-- 序号 -->
        <el-table-column
          type="index"
          label="序号"
          align="center"
          width="70px"
        ></el-table-column>
        <!-- 医院名称 -->
        <el-table-column
          prop="name"
          label="医院名称"
          align="center"
          width="200px"
        ></el-table-column>
        <!-- 医院简介 -->
        <el-table-column
          prop="introduce"
          label="医院简介"
          align="center"
          width="150px"
        >
        </el-table-column>
        <!-- 医院地址 -->
        <el-table-column
          prop="address"
          label="医院地址"
          align="center"
          width="220px"
        >
        </el-table-column>
        <!-- 标记 -->
        <el-table-column prop="flags" label="标记" align="center" width="220">
        </el-table-column>
        <!-- 经度 -->
        <el-table-column
          prop="longitude"
          label="经度"
          align="center"
          width="150px"
        >
        </el-table-column>
        <!-- 纬度 -->
        <el-table-column
          prop="latitude"
          label="纬度"
          align="center"
          width="150px"
        >
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="120">
          <template slot-scope="scope">
            <el-button @click="toEditNews(scope.row)" type="text" size="small"
              >修改</el-button
            >
            <el-button type="text" size="small" @click="toDelete(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page.sync="page"
      :page-size="pageSize"
      layout="total, prev, pager, next"
      :total="total"
    >
    </el-pagination>

     <!-- 模态框 -->
    <el-dialog
      v-if="dialogVisible"
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="50%"
    >
      <!-- 表单信息 -->
      <el-form
        ref="hosForm"
        :rules="hosRules"
        :model="hosForm"
        label-width="80px"
      >
        <!-- 名称 -->
        <el-form-item label="名称" prop="name">
          <el-input v-model="hosForm.name"></el-input>
        </el-form-item>
        <!-- 介绍 -->
        <el-form-item label="介绍" prop="introduce">
          <el-input type="textarea" v-model="hosForm.introduce"></el-input>
        </el-form-item>
        <!-- 省市 -->
        <el-form-item label="省市">
        <el-cascader
          v-model="hosForm.pca"
          :props="{ label: 'name', value: 'name' }"
          :options="pca"
        ></el-cascader>
        </el-form-item>
        <!-- 详细地址 -->
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="hosForm.address"></el-input>
        </el-form-item>

        <!-- 标记 -->
        <el-form-item label="标记" prop="flags">
          <el-input v-model="hosForm.flags"></el-input>
        </el-form-item>

        <!-- 经度 -->
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="hosForm.longitude"></el-input>
        </el-form-item>

        <!-- 纬度 -->
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="hosForm.latitude"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="toSubmitHos">提交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
//引入接口
import { get, post } from "@/utils/request";
//引入级联选择器
import pca from "@/assets/pca.json";

export default {
  data() {
    return {
      //医院表单对象
      hosForm: {},
      //医院表单的验证规则
      hosRules: {
        // 医院名称、医院地址、医院经纬度的表单验证规则
        name: [{ required: true, message: "请输入医院名称", trigger: "blur" }],
        address: [{ required: true, message: "请输入医院地址", trigger: "blur" }],
        longitude: [{ required: true, message: "请输入医院经度", trigger: "blur" }],
        latitude: [{ required: true, message: "请输入医院纬度", trigger: "blur" }],
      },
      allProData: [],
      // 要查询的医院名称
      hosName: "",
      // 医院的相关数据
      hosData: [],
      // 分页所需要的变量
      // 当前页码
      page: 1,
      // 每页要展示的数据条数
      pageSize: 10,
      // 数据总条数
      total: 10,
      dialogVisible: false,
      //设置模态框的标题
      dialogTitle: "",
      pca,
      //获取的省市信息
      proData:[]
    };
  },
  created() {
    // 自动获取医院的相关数据
    this.getHosData();
    this.getAllProData();
  },
  methods: {
    //获取医院数据
    async getHosData() {
      let params = {
        // 当前的页码
        page: this.page,
        // 每页要展示多少条数据
        pageSize: this.pageSize,
        // 查询的名称
        name: this.hosName,
      };
      //发送请求
      let res = await get("/hospital/pageQuery", params);
      // 赋值
      // console.log(res);
      this.hosData = res.data.list;
      // 获取到数据的总条数
      this.total = res.data.total;
    },

    //新增医院信息
    toAddHos() {
      //设置模态框的标题
      this.dialogTitle = "录入医院信息";
      this.dialogVisible = true;
      //清空表单对象中的数据
      this.hosForm = {};
      //弹出模态框
    },
    
    //修改医院信息
    toEditNews(row) {
      //设置模态框的标题
      this.dialogTitle = "修改医院信息";
      //将当前点击的这一行的数据 赋值给模态框里
      this.hosForm = { ...row };
      this.dialogVisible = true;
      //弹出模态框
    },

    //删除此条信息
    async toDelete(id) {
      //console.log(id);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          //发送请求
          let res = await get("/hospital/deleteById", { id });
          //console.log(res);
          if (res.status == 200) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            //刷新页面数据
            this.getHosData();
          } else {
            this.$message({
              type: "error",
              message: "删除失败!",
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    //分页
    handleCurrentChange() {
      this.page = val;
      this.getHosData();
    },

    //提交或修改医院信息
    toSubmitHos() {
      this.hosForm.province = this.hosForm.pca[0];
      this.hosForm.city = this.hosForm.pca[1];
      this.hosForm.area = this.hosForm.pca[2];

      this.$refs["hosForm"].validate(async (valid) => {
        if (valid) {
          //alert("submit!");
          //将表单对象提交到对应的接口中
          //提交成功之后给一个提示
          //失败也给一个提示
          //刷新页面
          console.log(this.hosForm);
          let res = await post("/hospital/saveOrUpdate", this.hosForm);
          if (res.status == 200) {
            this.$notify({
              title: "成功",
              message: res.message,
              type: "success",
            });
            this.dialogVisible = false
            // 刷新页面
            this.getHosData()
          } else {
            this.$notify({
              title: "失败",
              message: res.message,
              type: "error",
            });
          }
          //console.log(res);
        } else {
          //console.log("error submit!!");
          return false;
        }
      });
    },

  }
};
</script>

<style scoped>
/* 设置搜索区域的样式 */
.btn{
  display: flex;
  margin-left: 20px;
}
.searchArea,.addBtn{
  display: flex;
  margin-top: 5px;
  margin-left: 30px;
}
/* 设置搜索区域的按钮样式 */
.searchArea .searchBtn {
  margin-left: 5px;
}
</style>
